﻿using System.Linq;
using System.Web.Mvc;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using ASPItalia.ModelVirtualCasting.Web.Filters;
using ASPItalia.ModelVirtualCasting.Common;
using System;
using System.Collections.Generic;
using ASPItalia.ModelVirtualCasting.Web.Models;

namespace ASPItalia.ModelVirtualCasting.Web.Controllers
{
    [ProvideCountriesAttribute]
    public class CandidateController : Controller
    {
        private ICandidateRepository candidateRepository;

        #region refactoring per testability
        internal CandidateController(ICandidateRepository candidateRepository)
        {
            this.candidateRepository = candidateRepository;
        }

        public CandidateController()
            : this (ApplicationContext.Current.Candidates)
        { }
        #endregion

        #region Creazione candidate
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Candidate candidate)
        {
            if (!this.ModelState.IsValid)
            {
                return View();
            }
            
            ICandidateRepository repository = this.candidateRepository;
            repository.InsertOnSubmit(candidate);
            repository.SubmitChanges();

            TempData["Message"] = "Profilo salvato correttamente";

            return this.RedirectToAction("Profile", "Person", new { nickname = candidate.NickName });
        }
        #endregion

        #region Show profilo

        // su ProfileController

        //public ActionResult Profile(string nickname)
        //{
        //    var candidate = this.getCandidateByNick(nickname);
        //    this.ViewData.Model = candidate ?? new Candidate();

        //    if (candidate == null)
        //        this.TempData["Message"] = "Hai per caso provato a taroccare la URL???";

        //    #region AJAX
        //    if (Request.IsAjaxRequest())
        //        return this.View("ShowCandidate");
        //    #endregion

        //    return this.View();
        //}

        //public ActionResult ShowPics(string nickname)
        //{
        //    ICandidateRepository repository = this.candidateRepository;

        //    // Candidate candidate = repository.Include("Multimedia").GetByNickname(nickname);
        //    Candidate candidate = repository.Include("Multimedia").Where(c => c.NickName.ToLower() == nickname.ToLower()).Single();

        //    this.ViewData.Model = candidate;

        //    return this.View();
        //}
        #endregion

        #region Modifica profilo
        public ActionResult Edit(string nickname)
        {
            return this.View(this.getCandidateByNick(nickname));
        }

        [HttpPost]
        public ActionResult Edit(string nickname, Candidate candidate)
        {
            if (!this.ModelState.IsValid)
            {
                return View();
            }

            Candidate original = this.getCandidateByNick(nickname);
            this.UpdateModel(original, new string[] 
                { 
                    "FirstName",
                    "LastName",
                    "EmailAddress",
                    "BirthDate",
                    "CanBeNaked",
                    "Gender",
                    "PhysicalProfile"
                });
            original.ResidenceCountryID = candidate.ResidenceCountry.CountryID;
            original.NationalityCountryID = candidate.NationalityCountry.CountryID;

            ICandidateRepository repository = this.candidateRepository;
            repository.SubmitChanges();

            return this.RedirectToAction("Index", "Home");
        }

        private Candidate getCandidateByNick(string nickname)
        {
            ICandidateRepository repository = this.candidateRepository;
            // Candidate candidate = repository.GetByNickname(nickname);

            Candidate candidate = repository
                .Include("Multimedia").Include("NationalityCountry").Include("ResidenceCountry")
                .Where(c => c.NickName.ToLower() == nickname.ToLower()).SingleOrDefault();

            return candidate;
        }
        #endregion
    }
}
